package zjs.dc.controller;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

/**
 * @Class:
 * @Author: gaojian
 * @Description: 针对企查查t_eci_pledge表和中数t_stock_pledge表进行数据映射开发
 * @Date: 2021/12/29
 */

public class TEciPledge {
    public static void main(String[] args) {
        SparkSession spark = SparkSession.builder().appName("t_eci_pledge").enableHiveSupport().getOrCreate();
        spark.sparkContext().setLogLevel("ERROR");
        Dataset<Row> zs_data = spark.sql("select * from a_dc_ep_ods.zs_t_stock_pledge");
        long zs_count = zs_data.count();
        if(zs_count>0){

            // 处理步骤说明 1.UPDATE 2.DELETE
            // 1.UPDATE
            spark.sql("    select del.creditcode,p.* from a_dc_ep_ods.zs_t_stock_pledge p\n" +
                    "    inner join a_dc_ep_ods.zs_t_stock_pledge_del del on del.entid = p.entid\n" +
                    "    where del.creditcode is not null and del.creditcode != ''\n").createOrReplaceTempView("tmp_table");

            Dataset<Row> update_data = spark.sql("SELECT \n" +
                    "md5(CONCAT(UPPER(TRIM(A.CREDITCODE)),UPPER(TRIM(A.STK_PAWN_REGNO)),UPPER(TRIM(A.STK_PAWN_CZCERNO)),UPPER(TRIM(A.STK_PAWN_ZQCERNO)))) AS ID,\n" +
                    "B.KEY_NO,\n" +
                    "B.COMPANY_ID,\n" +
                    "B.COMPANY_NAME,\n" +
                    "A.STK_PAWN_REGNO AS REGIST_NO,\n" +
                    "A.STK_PAWN_CZCERNO AS PLEDGOR_NO,\n" +
                    "A.STK_PAWN_CZPER AS PLEDGEOR,\n" +
                    "A.STK_PAWN_ZQCERNO AS PLEDGEE_NO,\n" +
                    "A.STK_PAWN_ZQPER AS PLEDGEE,\n" +
                    "A.STK_PAWN_CZAMT AS PLEDGED_AMOUNT,\n" +
                    "A.STK_PAWN_REGDATE AS REG_DATE,\n" +
                    "A.STK_PAWN_DATE AS PUBLIC_DATE,\n" +
                    "A.STK_PAWN_STATUS AS STATUS,\n" +
                    "A.JOBID, '0' AS ISADD \n" +
                    "FROM \n" +
                    "tmp_table A INNER JOIN \n" +
                    "a_dc_ep_ods.t_eci_company B \n" +
                    "ON UPPER(TRIM(B.CREDIT_CODE)) = UPPER(TRIM(A.CREDITCODE))");
            update_data.createOrReplaceTempView("tmp_t_eci_pledge");
            spark.sql("insert overwrite table a_dc_ep_incr.t_eci_pledge select * from tmp_t_eci_pledge");


            // 2.DELETE
            spark.sql("select del.creditcode,del.jobid from a_dc_ep_ods.zs_t_stock_pledge_del del " +
                    "left join a_dc_ep_ods.zs_t_stock_pledge p on del.entid = p.entid " +
                    "where p.entid is null and del.creditcode != '' and del.creditcode is not null").createOrReplaceTempView("tmp_table1");
            spark.sql("select d.*,c.credit_code from a_dc_ep_dwi.t_eci_pledge d inner join a_dc_ep_ods.t_eci_company c on d.company_id = c.company_id")
                    .createOrReplaceTempView("tmp_table7");
            Dataset<Row> delete_data = spark.sql("select \n" +
                    "p.id,\n" +
                    "p.key_no,\n" +
                    "p.company_id,\n" +
                    "p.company_name,\n" +
                    "p.regist_no,\n" +
                    "p.pledgor_no,\n" +
                    "p.pledgor,\n" +
                    "p.pledgee_no,\n" +
                    "p.pledgee,\n" +
                    "p.pledged_amount,\n" +
                    "p.reg_date,\n" +
                    "p.public_date,\n" +
                    "p.status,\n" +
                    "del.jobid,'-1' as isadd\n" +
                    "from tmp_table1 del\n" +
                    "inner join tmp_table7 p on upper(trim(p.credit_code)) = upper(trim(del.creditcode))");
            delete_data.createOrReplaceTempView("tmp_t_eci_pledge2");
            spark.sql("insert into table a_dc_ep_incr.t_eci_pledge select * from tmp_t_eci_pledge2");
        }else {
            spark.sql("TRUNCATE TABLE a_dc_ep_incr.t_eci_pledge");
            System.out.println("中数本期数据涉及该表的数据为空。。。。。。");
        }

        spark.stop();
    }
}
